package com.example.newsapp.ui.components.mainComponent

import android.util.Log
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.example.newsapp.R
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave

/**
 * 底部导航栏
 *
 * @param navController 导航控制器
 */
@Composable
fun BottomNavigationBar(
    navController: NavHostController,
    modifier: Modifier = Modifier
) {
    // 选择状态
    NavigationBar(
        modifier = Modifier.height(120.dp),
    ) {
        // 新闻标签
        NavigationBarItem(
            selected = StateSave.selected == 0,
            onClick = {
                StateSave.previousSelect = StateSave.selected
                StateSave.selected = 0
                StateSave.currentSelect = StateSave.selected
                BottomNavtionSelected(
                    navController = navController,
                    route = PageNavigation.newsScreen
                )
            },
            icon = {
                Icon(painterResource(R.drawable.baseline_newspaper_24), null)
            },
            label = {
                Text(stringResource(R.string.news))
            },
            colors = NavigationBarItemDefaults
                .colors(selectedIconColor = Color(0xC9F13141), selectedTextColor = Color(0xC9F13141))
        )
        // 图片标签
        NavigationBarItem(
            selected = StateSave.selected == 1,
            onClick = {
                StateSave.previousSelect = StateSave.selected
                StateSave.selected = 1
                StateSave.currentSelect = StateSave.selected
                BottomNavtionSelected(
                    navController = navController,
                    route = PageNavigation.pictureScreen
                )
                StateSave.isNewsPage = false
            },
            icon = {
                Icon(painterResource(R.drawable.baseline_picture_as_pdf_24), null)
            },
            label = {
                Text(stringResource(R.string.picture))
            },
            colors = NavigationBarItemDefaults
                .colors(selectedIconColor = Color(0xC9F13141), selectedTextColor = Color(0xC9F13141))
        )
        // 天气标签
        NavigationBarItem(
            selected = StateSave.selected == 2,
            onClick = {
                StateSave.previousSelect = StateSave.selected
                StateSave.selected = 2
                StateSave.currentSelect = StateSave.selected
                BottomNavtionSelected(
                    navController = navController,
                    route = PageNavigation.weatherScreen
                )
                StateSave.isNewsPage = false
            },
            icon = {
                Icon(painterResource(R.drawable.baseline_cloud_24), null)
            },
            label = {
                Text(stringResource(R.string.weather))
            },
            colors = NavigationBarItemDefaults
                .colors(selectedIconColor = Color(0xC9F13141), selectedTextColor = Color(0xC9F13141))
        )
        // 我的标签
        NavigationBarItem(
            selected = StateSave.selected == 3,
            onClick = {
                StateSave.previousSelect = StateSave.selected
                StateSave.selected = 3
                StateSave.currentSelect = StateSave.selected
                BottomNavtionSelected(
                    navController = navController,
                    route = PageNavigation.accountScreen
                )
                StateSave.isNewsPage = false
            },
            icon = {
                Icon(painterResource(R.drawable.baseline_manage_accounts_24), null)
            },
            label = {
                Text(stringResource(R.string.account))
            },
            colors = NavigationBarItemDefaults
                .colors(selectedIconColor = Color(0xC9F13141), selectedTextColor = Color(0xC9F13141))
        )
    }
}

// 导航选项
fun BottomNavtionSelected(
    navController: NavHostController,
    route: String,
    modifier: Modifier = Modifier
) {
    navController.navigate(
        route =route
    ) {
        launchSingleTop = true
        restoreState = true
        popUpTo(navController.graph.id) {
            saveState = true
        }
    }
}